@file:Suppress("SpellCheckingInspection")

package han.cirno.corrupt.components.school.impl.fjut

import han.cirno.corrupt.data.instances.UserExamData
import han.cirno.corrupt.util.RegexUtil
import org.json.JSONArray
import org.json.JSONObject

object FjutExamResolver {
    fun analyze(jsonString: String):List<UserExamData.Exam>{
        val jsonObject = JSONObject(jsonString)
        val itemsArray = jsonObject["items"] as JSONArray
        val examsList:MutableList<UserExamData.Exam> =ArrayList()
        for(i in 0 until itemsArray.length()){
            val value = itemsArray[i] as JSONObject
            val name=value["kcmc"].toString()
            val timeRaw=value["kssj"].toString()
            val place=value["cdxqmc"].toString()
            val room=value["cdmc"].toString()
            val seat=value["zwh"].toString()
            val date= RegexUtil.matchFirst("(?<=\\().*?(?=\\))",timeRaw)?:""
            val dateSplit=date.split("-")
            val year=dateSplit[0]
            val month=dateSplit[1]
            val day=dateSplit[2]
            val time= RegexUtil.matchFirst("(?<=\\) )\\d*?\\:\\d*-\\d*\\:\\d*",timeRaw)?:""
            val fromAndToTime = time.split("-")
            val fromTime=fromAndToTime[0].split(":")
            val toTime=fromAndToTime[1].split(":")
            examsList.add(UserExamData.Exam(
                name,
                Triple(year,month,day),
                Pair(fromTime[0],fromTime[1]),Pair(toTime[0],toTime[1]),
                "$place-$room",seat))
        }
        return examsList
    }
}